Skip to content

[COLLECTIONS-811] Integrate Guava Testlib tests for Apache Commons Collections#301

Merged
garydgregory merged 2 commits intoapache:masterfrom
kinow:COLLECTIONS-811
Jun 6, 2022
Merged

[COLLECTIONS-811] Integrate Guava Testlib tests for Apache Commons Collections#301
garydgregory merged 2 commits intoapache:masterfrom
kinow:COLLECTIONS-811

Conversation

@kinow
Copy link
Member

@kinow kinow commented Apr 26, 2022

Adding the test class from @ben-manes, pending merge of #300

@ben-manes
Copy link

/cc @kevinb9n 🙂

@kinow kinow force-pushed the COLLECTIONS-811 branch from 8405c10 to 745f45f Compare April 28, 2022 10:40
@kinow kinow marked this pull request as ready for review April 28, 2022 10:40
@kinow
Copy link
Member Author

kinow commented Apr 28, 2022

Rebased. Confirmed the license is compatible (AL as well). Waiting for CI, and will update changes.xml in the meantime. Then it should be ready for review 👍

@kinow
Copy link
Member Author

kinow commented Apr 28, 2022

And didn't notice a difference in build time. Executed a few times locally as well, and can confirm it doesn't bring any brittleness to our tests. Looks like a good addition to our test code. Thanks for showing us this @ben-manes !

*/
public final class GuavaTestlibTest extends TestCase {

public static Test suite() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be done in the JUnit 4 or 5 style?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that too, but I don't know how to translate the test suite created programmatically to JUnit 5 😥 ping @ben-manes

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The collection tests are JUnit3-based, and JUnit 4/5 have runners for supporting their older versions. Since those versions are not backwards compatible otherwise, it's a framework limitation. I think it is unlikely for Guava to revise the tests as it works well enough, even if old code.

FYI there are more test cases you could add (List, Set, etc) and I only did a quick check for Maps. (I also borrowed your tests for my implementation as another sanity check)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ben-manes !

I will leave the List, Set, etc., for follow-up issues. I had some 5 minutes to test it, and managed to write some tests for Lists, but these tests found issues in some of the Lists. I'd have to confirm if the features I selected are actually pertinent to the List implementations I used, or if we have other bugs 🙂

So it's definitely useful, but I prefer to get this merged first for Maps, and later add other Map, List, Set implementations too 👍

Here's the diff of what I had FWIW, thanks!!!
Bruno

diff --git a/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java b/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java
index 88108c6a..7d068e17 100644
--- a/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java
+++ b/src/test/java/org/apache/commons/collections4/GuavaTestlibTest.java
@@ -17,18 +17,25 @@
 
 package org.apache.commons.collections4;
 
+import java.util.List;
 import java.util.Map;
 import java.util.function.Supplier;
 
+import org.apache.commons.collections4.list.CursorableLinkedList;
+import org.apache.commons.collections4.list.GrowthList;
+import org.apache.commons.collections4.list.TreeList;
 import org.apache.commons.collections4.map.HashedMap;
 import org.apache.commons.collections4.map.LRUMap;
 import org.apache.commons.collections4.map.LinkedMap;
 import org.apache.commons.collections4.map.ReferenceMap;
 
+import com.google.common.collect.testing.ListTestSuiteBuilder;
 import com.google.common.collect.testing.MapTestSuiteBuilder;
+import com.google.common.collect.testing.TestStringListGenerator;
 import com.google.common.collect.testing.TestStringMapGenerator;
 import com.google.common.collect.testing.features.CollectionFeature;
 import com.google.common.collect.testing.features.CollectionSize;
+import com.google.common.collect.testing.features.ListFeature;
 import com.google.common.collect.testing.features.MapFeature;
 
 import junit.framework.Test;
@@ -49,14 +56,17 @@ public final class GuavaTestlibTest extends TestCase {
 
     public static Test suite() {
         TestSuite test = new TestSuite();
-        test.addTest(suite("HashedMap", HashedMap::new));
-        test.addTest(suite("LinkedMap", LinkedMap::new));
-        test.addTest(suite("LRUMap", LRUMap::new));
-        test.addTest(suite("ReferenceMap", ReferenceMap::new));
+        test.addTest(suiteMap("HashedMap", HashedMap::new));
+        test.addTest(suiteMap("LinkedMap", LinkedMap::new));
+        test.addTest(suiteMap("LRUMap", LRUMap::new));
+        test.addTest(suiteMap("ReferenceMap", ReferenceMap::new));
+        test.addTest(suiteList("List", TreeList::new));
+        test.addTest(suiteList("GrowthList", GrowthList::new));
+        test.addTest(suiteList("CursorableLinkedList", CursorableLinkedList::new));
         return test;
     }
 
-    public static Test suite(String name, Supplier<Map<String, String>> factory) {
+    public static Test suiteMap(String name, Supplier<Map<String, String>> factory) {
         return MapTestSuiteBuilder.using(new TestStringMapGenerator() {
             @Override
             protected Map<String, String> create(Map.Entry<String, String>[] entries) {
@@ -73,4 +83,23 @@ public final class GuavaTestlibTest extends TestCase {
                         MapFeature.ALLOWS_ANY_NULL_QUERIES, CollectionFeature.SUPPORTS_ITERATOR_REMOVE)
                 .createTestSuite();
     }
+
+    public static Test suiteList(String name, Supplier<List<String>> factory) {
+        return ListTestSuiteBuilder.using(new TestStringListGenerator() {
+            @Override
+            protected List<String> create(String[] elements) {
+                List<String> list = factory.get();
+                for (String element : elements) {
+                    list.add(element);
+                }
+                return list;
+            }
+        })
+                .named(name)
+                .withFeatures(
+                        CollectionSize.ANY, ListFeature.GENERAL_PURPOSE,
+                        ListFeature.REMOVE_OPERATIONS, ListFeature.SUPPORTS_REMOVE_WITH_INDEX,
+                        CollectionFeature.SUPPORTS_ITERATOR_REMOVE)
+                .createTestSuite();
+    }
 }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can get a little further using the following, where TreeList passes but the others don't. The remaining failures look like actual mistakes.

public static Test suite() {
  TestSuite test = new TestSuite();
  test.addTest(suiteList("TreeList", TreeList::new));
  test.addTest(suiteList("GrowthList", GrowthList::new, CollectionFeature.SERIALIZABLE));
  test.addTest(suiteList("CursorableLinkedList", CursorableLinkedList::new, CollectionFeature.SERIALIZABLE));
  return test;
}

public static Test suiteList(String name, Supplier<List<String>> factory, Feature<?>... features) {
  var suite = ListTestSuiteBuilder.using(new TestStringListGenerator() {
    @Override
    protected List<String> create(String[] elements) {
      List<String> list = factory.get();
      for (String element : elements) {
        list.add(element);
      }
      return list;
    }
  }).named(name)
      .withFeatures(CollectionSize.ANY,
          ListFeature.GENERAL_PURPOSE,
          ListFeature.REMOVE_OPERATIONS,
          CollectionFeature.ALLOWS_NULL_VALUES,
          CollectionFeature.DESCENDING_VIEW,
          CollectionFeature.SUBSET_VIEW);
  suite.withFeatures(features);
  return suite.createTestSuite();
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, same for me with your code. I'm updating the PR now with the suiteMap and suiteTest methods, but leaving only TreeList tests enabled for now. I left a TODO marker so we can revisit it later.

Thanks!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least in the case of GrowthList some of the failures are expected since it modified the List contract,

 * Decorates another <code>List</code> to make it seamlessly grow when
 * indices larger than the list size are used on add and set,
 * avoiding most IndexOutOfBoundsExceptions.

Technically a violation of the contract, but expected.

 * @throws IndexOutOfBoundsException if the index is out of range
 *         ({@code index < 0 || index > size()})
 */
void add(int index, E element);

You can use .suppressing(Method...) to disable those cases.

@kinow kinow force-pushed the COLLECTIONS-811 branch from 745f45f to ba25788 Compare May 1, 2022 00:08
@kinow
Copy link
Member Author

kinow commented May 1, 2022

Rebased.

@codecov-commenter
Copy link

codecov-commenter commented May 1, 2022

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.06%. Comparing base (5286994) to head (eff4e6d).
Report is 763 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #301      +/-   ##
============================================
+ Coverage     85.95%   86.06%   +0.10%     
- Complexity     4670     4672       +2     
============================================
  Files           292      292              
  Lines         13467    13467              
  Branches       1954     1954              
============================================
+ Hits          11576    11590      +14     
+ Misses         1325     1320       -5     
+ Partials        566      557       -9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

* This test uses Google's Guava Testlib testing libraries to validate the
* contract of collection classes in Commons Collections. This was introduced
* after COLLECTIONS-802, where the issue reported was found with Testlib,
* with thanks to Ben Manes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use @author tags, so would you please move attribution to changes.xml and/or pom.xml?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine having it removed and appreciate the thanks.

Copy link
Member Author

@kinow kinow Jun 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done @garydgregory, and rebased! Thanks

@kinow kinow force-pushed the COLLECTIONS-811 branch from 919b449 to eff4e6d Compare June 6, 2022 21:04
@garydgregory garydgregory merged commit 06f7b6f into apache:master Jun 6, 2022
anantdamle pushed a commit to anantdamle/commons-collections that referenced this pull request Aug 14, 2023
…llections (apache#301)

* [COLLECTIONS-811] Integrate Guava Testlib tests for Apache Commons Collections

* [COLLECTIONS-811] Add tests for Lists too, thanks to @ben-manes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants